package neusoft.example.com.yuejian06.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import neusoft.example.com.yuejian06.Activity.ActivityDetailActivity;
import neusoft.example.com.yuejian06.Activity.PictureDetailActivity;
import neusoft.example.com.yuejian06.Bean.Picture;
import neusoft.example.com.yuejian06.Fragment.PictureListFragment;
import neusoft.example.com.yuejian06.PublicActivity;
import neusoft.example.com.yuejian06.R;

public class PictureListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private LayoutInflater inflater;
    private List<Picture> list;
    private Context context;
    private PublicActivity publicActivity;


    public PictureListAdapter(Context context)
    {
        this.inflater = LayoutInflater.from(context);
        this.context=context;
    }

    public PictureListAdapter(Context context, List<Picture> list) {
        this.context = context;
        this.list = list;
    }


    public void setList(List<Picture> list){
        this.list = list;
        notifyDataSetChanged();
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.adapter_picturelist, parent, false);
        RecyclerView.ViewHolder viewHolder = new ViewHolder(itemView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        publicActivity=new PublicActivity();
        final Picture bean = list.get(position);

        if(bean == null) return;
        final ViewHolder viewHolder = (ViewHolder) holder;

        viewHolder.user_name.setText(bean.getUser_name());
        viewHolder.picture_title.setText("#"+bean.getPicture_title()+"#");
        viewHolder.picture_time.setText(bean.getPicture_time());

        Picasso.with(context)
                .load(publicActivity.getIp()+"static/user_picture/"+bean.getUser_pic())
                .into(viewHolder.user_pic);
        Picasso.with(context)
                .load(publicActivity.getIp()+"static/picture/"+bean.getPicture_pic())
                .into(viewHolder.picture_pic);




        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int position = viewHolder.getLayoutPosition();
                Intent intent = new Intent(context, PictureDetailActivity.class);
                intent.putExtra("picture_id",bean.getPicture_id());
                context.startActivity(intent);
            }
        });




    }

    @Override
    public int getItemCount() {
        if(list == null)
            return 0;
        else
            return list.size();
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView user_pic;
        ImageView picture_pic;
        TextView user_name;
        TextView picture_title;
        TextView picture_time;
        ImageView image_camera;

        public ViewHolder(View itemView) {
            super(itemView);
            user_pic = (ImageView) itemView.findViewById(R.id.user_pic);
            picture_pic = (ImageView) itemView.findViewById(R.id.picture_pic);
            image_camera=(ImageView) itemView.findViewById(R.id.image_camera);
            user_name = (TextView) itemView.findViewById(R.id.user_name);
            picture_title = (TextView) itemView.findViewById(R.id.picture_title);
            picture_time = (TextView) itemView.findViewById(R.id.picture_time);

        }
    }


}
